﻿using System;
using System.Drawing;
using System.Windows.Forms;

// 算法框架类
public class AlgorithmFramework
{
    // 绘制函数图形的方法
    public void DrawGraph(Func<double, double> function)
    {
        // 创建一个窗体来显示图形
        Form form = new Form();
        form.Width = 800;
        form.Height = 600;
        form.Paint += (sender, e) =>
        {
            Graphics g = e.Graphics;
            g.Clear(Color.White);

            // 绘制函数图形
            for (int x = 0; x < form.Width; x++)
            {
                double y = function(x / 100.0);
                int yCoord = (int)(form.Height - y * 300);
                if (x == 0)
                {
                    g.DrawLine(Pens.Black, x, yCoord, x + 1, yCoord);
                }
                else
                {
                    g.DrawLine(Pens.Black, x, yCoord, x + 1, yCoord);
                }
            }
        };
        form.Show();
    }
}

// 模拟计算类
public class SimulationCalculator
{
    // 计算进度事件
    public event EventHandler<ProgressChangedEventArgs> ProgressChanged;

    // 触发进度事件的方法
    protected virtual void OnProgressChanged(double progress)
    {
        ProgressChanged?.Invoke(this, new ProgressChangedEventArgs(progress));
    }

    // 模拟计算方法
    public void SimulateCalculation()
    {
        for (double i = 0; i <= 1; i += 0.01)
        {
            // 模拟计算
            double result = Math.Sin(i * Math.PI);

            // 反馈计算进度
            OnProgressChanged(i * 100);
            System.Threading.Thread.Sleep(10); // 模拟耗时操作
        }
    }
}

// 进度事件参数类
public class ProgressChangedEventArgs : EventArgs
{
    public double Progress { get; }

    public ProgressChangedEventArgs(double progress)
    {
        Progress = progress;
    }
}

// 主程序类
class Program
{
    static void Main(string[] args)
    {
        // 创建算法框架实例
        AlgorithmFramework framework = new AlgorithmFramework();

        // 测试绘制正弦函数图形
        framework.DrawGraph(x => Math.Sin(x * Math.PI));

        // 创建模拟计算类实例
        SimulationCalculator calculator = new SimulationCalculator();
        calculator.ProgressChanged += (sender, e) =>
        {
            Console.WriteLine($"Progress: {e.Progress:F2}%");
        };

        // 执行模拟计算方法
        calculator.SimulateCalculation();

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }
}